//
//  YZYUserInfo.m
//  YZYSDKDemo
//
//  Created by 赵隆杰 on 2024/6/25.
//

#import "YZYUserInfo.h"
#import "ZCFunDataFormat.h"
#import "YZYSaccountStore.h"

@implementation YZYUserInfo

- (instancetype)initWithUserDict:(NSDictionary *)dict {
    if (self == [super init]) {
        YZYUserInfo *userInfo = [[YZYUserInfo alloc] init];
        userInfo.accessToken = [ZCFunDataFormat filterString:dict[@"accessToken"]];
        userInfo.isAuth = [ZCFunDataFormat filterNumber:dict[@"isAuth"]];
        userInfo.isPhone = [ZCFunDataFormat filterNumber:dict[@"isPhone"]];
        userInfo.isRegister = [ZCFunDataFormat filterNumber:dict[@"isRegister"]];
        userInfo.userId = [ZCFunDataFormat filterNumber:dict[@"userId"]];
        userInfo.userName = [ZCFunDataFormat filterString:dict[@"userName"]];
        userInfo.uuid = [ZCFunDataFormat filterString:dict[@"uuid"]];
        userInfo.sonId = [ZCFunDataFormat filterNumber:dict[@"sonId"]];
        return userInfo;
    }
    return self;
}

- (void)saveUserInfo {
    NSMutableDictionary *userDict = [NSMutableDictionary dictionary];
    [userDict setObject:self.accessToken forKey:@"accessToken"];
    [userDict setObject:self.isAuth forKey:@"isAuth"];
    [userDict setObject:self.isPhone forKey:@"isPhone"];
    [userDict setObject:self.isRegister forKey:@"isRegister"];
    [userDict setObject:self.userId forKey:@"userId"];
    [userDict setObject:self.userName forKey:@"userName"];
    [userDict setObject:self.uuid forKey:@"uuid"];
    [userDict setObject:self.sonId forKey:@"sonId"];
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userDict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    if (!error) {
        NSString *userInfoStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        [[YZYSaccountStore sharedInstance] saveUserInfoStr:userInfoStr];
        self.isLogin = YES;
    } else {
        NSLog(@"Error: %@", error.localizedDescription);
    }
    
}

- (void)removeUserInfo {
    [[YZYSaccountStore sharedInstance] removeUserInfoStr];
    self.isLogin = NO;
}

- (YZYUserInfo *)getUserInfo {
    if (self.isLogin == NO) {
        return nil;
    }
    return self;
}

@end
